package com.lanou.beautifulwardrobe.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.WaterfallCategory;
import com.lanou.beautifulwardrobe.listener_interface.MyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class WaterfallRecyclerViewAdapter extends RecyclerView.Adapter<WaterfallRecyclerViewAdapter.WaterfallViewHolder> {
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private List<WaterfallCategory> datas;
    private MyItemClickListener listener;
    private ImageLoader imageLoader;
    public void setOnMyItemClickListener(MyItemClickListener listener) {
        this.listener = listener;
    }

    public void addData(List<WaterfallCategory> waterfallCategories) {
        datas = waterfallCategories;
        notifyDataSetChanged();
    }

    @Override
    public WaterfallViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        View v = LayoutInflater.from(view.getContext()).inflate(R.layout.recommend_waterfall_item, null);
        return new WaterfallViewHolder(v);
    }

    @Override
    public void onBindViewHolder(WaterfallViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {

            WaterfallCategory waterfallCategory = datas.get(position);
            holder.position = position;
            holder.englishTitle.setText(waterfallCategory.getEn_taobao_title());
            holder.chineseTitle.setText(waterfallCategory.getTaobao_title());
            String imageUrl = waterfallCategory.getBackground_pic_url();
            getBitMap(imageUrl, holder.backgroundImage);

        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class WaterfallViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView chineseTitle;
        private TextView englishTitle;
        private NetworkImageView backgroundImage;
        private FrameLayout waterfallLayout;


        private int position;

        public WaterfallViewHolder(View view) {
            super(view);

            backgroundImage = (NetworkImageView) view.findViewById(R.id.backgroundImage);
            chineseTitle = (TextView) view.findViewById(R.id.chinsesTitle);
            englishTitle = (TextView) view.findViewById(R.id.englishTitle);
            waterfallLayout = (FrameLayout) view.findViewById(R.id.waterfallLayout);

            waterfallLayout.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            if (datas != null && datas.size() > 0) {

                String titleName = datas.get(position).getTaobao_title();
                    if (listener != null) {
                        listener.onItemClick(titleName);
                    }
            }
        }
    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
